#include					"Goban.hpp"
#include					<iostream>

Goban::Goban(std::map<int, Square *> &squareMap)
	: _turn(0), _mapInfo(new Box[19*19]()), _squareMap(squareMap)
{
}

Goban::Goban(const Goban & other)
 : _squareMap(other._squareMap)
{
 if (this != &other)
 {
  _turn = other._turn;
  _players = other._players;
  _arbitre = other._arbitre;
  _gameType = other._gameType;
  _winner = other._winner;
  _mapInfo = other._mapInfo;
 }

}

Goban::~Goban()
{
}

void						Goban::display()
{
	int						j = 1;
	for (int i = 0; i <= 360; ++i)
	{
	/*	unsigned int bit = 0;
		for (int i = 7 ; i >= 0 ; --i)
		{
			bit = (_winner >> i) & 1;
			std::cout << bit;
			if (i % 8 == 0 && i < 7)
				std::cout << " ";
		}*/
		if (_mapInfo[i].directions == 0)
			std::cout << "0 ";
		else
			std::cout << "1 ";
		if (!(j++ % 19))
			std::cout << std::endl;	
	}
	std::cout << std::endl;	
}

void					Goban::init(char type)
{
	_gameType = (type >> 0) & 1;
	_arbitre = new Arbitre(getSquareMap(), ((type >> 1) & 1), ((type >> 2) & 1));
	_players.push_back(new Human(*this, WHITE_STONE_PATH, *_arbitre));
	if (!_gameType)
		_players.push_back(new Human(*this, BLACK_STONE_PATH, *_arbitre));
	else
		_players.push_back(new IA(*this, BLACK_STONE_PATH, *_arbitre, 4));
}

void						Goban::playersTurn(int key)
{ 
	_players[_turn++ % 2]->putStone(key);
//	display();
	if (_gameType)
		_players[_turn++ % 2]->putStone(key);
}

void						Goban::checkCoordsValidity(int key)
{
	if (!_squareMap[key]->displayable() && KEY_VALIDITY(key))
		playersTurn(key);
}

void						Goban::putStone(int key)
{
	checkCoordsValidity(key);
	//display();
}

bool						Goban::isThereAWinner()
{
	if (_winner = _arbitre->getWin())
		return (true);
	return (false);
}

void					Goban::setSquareMap(std::map<int, Square*> &squareMap)
{
	_squareMap = squareMap;
}

std::string				Goban::winScreen()
{
	return (WHO_WON(_winner));
}

std::map<int, Square*>  &Goban::getSquareMap()
{
	return (_squareMap);
}

Box						&Goban::getSquareInfo(int key)
{
	return (_mapInfo[key]);
}

Box						*Goban::getMapInfo()
{
	return (_mapInfo);
}

char const				Goban::getGameType() const
{
	return (0);
}